<template>
  <div class="wrap">
    <div id="container2" />
    <div class="lengend">
      <div class="lengend-title">{{ $route.query.name || '' }}</div>
      <div class="lengend-content">
        <div v-for="(item ,idx) of lengendList" :key="idx" class="lengend-item">
          <div class="item-img">
            <img :src="item.url">
          </div>
          <div class="item-number">{{ item.number }}</div>
          <div class="item-name">{{ item.name }}</div>
        </div>
      </div>
    </div>
  </div>

</template>
<script>
import * as THREE from 'three'
import { Child } from './obj/screen'
import { IOTdata } from './data/mockData'
export default {
  name: 'Screen2',
  components: {},
  props: {
    renderData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      lengendList: [
        {
          url: 'static/img/icon-camera.png',
          number: 3,
          name: '摄像头'
        },
        {
          url: 'static/img/icon-air.png',
          number: 2,
          name: '空气温湿度测量设备'
        },
        {
          url: 'static/img/icon-soil.png',
          number: 2,
          name: '土壤温湿度测量仪器'
        },
        {
          url: 'static/img/icon-light.png',
          number: 1,
          name: '光照测量设备'
        },
        {
          url: 'static/img/all.png',
          number: 5,
          name: '物联网设备'
        }
      ],
      data: [
        {
          // 暂时无用
          area: '6亩',
          equipment: '42个',
          Yield: '5000KG',
          seed: '3种',
          name: '大棚A'
        }
      ],
      IOTdata
    }
  },
  watch: {
    renderData(value) {
      // this.delData()
    }
  },
  mounted() {
    this.init()
    this.delData()
    window.addEventListener('resize', this.onWindowResize, false)
    // document.getElementById('container2').addEventListener('click', this.onMouseClick, false)
    document.getElementById('container2').addEventListener('mousemove', this.onMouseClick, false)
  },
  methods: {
    delData() {
      // IOTdata[0] 摄像头1
      // IOTdata[1] 摄像头2
      // IOTdata[2] 摄像头3
      // IOTdata[3] 空气温湿设备1
      // IOTdata[4] 空气温湿设备2
      // IOTdata[5] 土壤温湿设备1
      // IOTdata[6] 土壤温湿设备2
      // IOTdata[7] 光照测量设备
      // 添加大棚
      this.scene.addObj(this.data, IOTdata)
    },
    init() {
      this.scene = new Child('container2')
      this.raycaster = new THREE.Raycaster()
      this.mouse = new THREE.Vector2()
    },
    onWindowResize() {
      this.scene.camera.instance.aspect = window.innerWidth / window.innerHeight
      this.scene.camera.instance.updateProjectionMatrix()
      this.scene.renderer.setSize(window.innerWidth, window.innerHeight)
    },
    onMouseClick(event) {
      event.preventDefault()
      const width = document.getElementById('container2').offsetWidth
      const height = document.getElementById('container2').offsetHeight
      const left = document.getElementById('container2').getBoundingClientRect().left
      const top = document.getElementById('container2').getBoundingClientRect().top
      this.mouse.x = ((event.clientX - left) / width) * 2 - 1
      this.mouse.y = -((event.clientY - top) / height) * 2 + 1
      this.updateMouse(event)
    },
    updateMouse(event) {
      this.raycaster.setFromCamera(this.mouse, this.scene.camera.instance)
      // // 地图块的
      // console.log(this.scene.getEventGroup(), 'this.scene.getEventGroup()')
      const intersects = this.raycaster.intersectObjects(this.scene.getEventGroup())
      // 设置鼠标样式
      this.scene.renderer.domElement.style.cursor = 'auto'
      // // 点击时
      if (intersects.length > 0) {
        const selectName = intersects[0].object.name
        this.scene.select(selectName)
        this.scene.renderer.domElement.style.cursor = 'pointer'
        // if (event.type === 'click') {
        //   this.$router.push({
        //     path: '/child',
        //     query: {
        //       id: 111
        //     }
        //   })
        // }
        // this.setMap(inSt.object.infoData)
      } else {
        this.scene.select('')
      }
    }
  }
}
</script>

<style scoped lang="less" >
.wrap {
  width:100%;
  height:100%;
  position: relative;
}
#container2{
  width:100%;
  height:100%;
}
.lengend {
  transform-origin: left top;
  position: absolute;
  width: 251px;
  height: 294px;
  left: 25%;
  top: 90px;
  background-image: url("~@/assets/lengend/lenegnd-bg.png");
  background-size: 100% 100%;
  z-index: 999;
  .lengend-title {
    height: 27px;
    line-height: 27px;
    font-size: 18px;
    font-family: Microsoft YaHei;
    padding-left: 35px;
    padding-top: 3px;
    font-weight: 400;
    color: #FFFFFF;
    margin-bottom: 10px;
  }
  .lengend-content {
    height: calc(100% - 40px);
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .lengend-item {
      height: 40px;
      display: flex;
      padding-left: 10px;
      .item-img {
        width: 40px;
        height: 40px;
        background-image: url("~@/assets/lengend/block-bg.png");
        background-size: 100% 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        img {
          width: 20px;
          height: 20px;
        }
      }
      .item-number {
        margin-left: 10px;
        font-size: 22px;
        line-height: 40px;
        font-weight: bold;
        background-image:-webkit-linear-gradient(bottom,#40DEDD,#40DEDD,#fff, #fff);
        -webkit-background-clip:text;
        -webkit-text-fill-color:transparent;
      }
      .item-name {
        margin-left: 5px;
        font-size: 14px;
        color: #FFFFFF;
        line-height: 40px;
      }
    }
  }
}
@media screen and (min-width: 1500px) {
  .lengend {
    transform: scale(1, 1)
  }
}
@media screen and (max-width: 1500px) {
  .lengend {
    transform: scale(0.6, 0.6)
  }
}
</style>
